﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DM4;
using UrielGuy.SyntaxHighlightingTextBox;
using sscore;

namespace DM4Client.AddEditForms
{
    public partial class FrmAddEditMailTemplate : DevExpress.XtraEditors.XtraForm
    {

        public int MailTemplateId = DM4Consts.DefaultNULLId;
        public OperationResult Result = new OperationResult();
        public const string FrmCaption = "New mail template adding";

        public SyntaxHighlightingTextBox StbMailSendTemplateSubject;
        public SyntaxHighlightingTextBox StbMailSendTemplateBody;

        public Params Prm;

        public FrmAddEditMailTemplate(Params prm, int mailTemplateId = DM4Consts.DefaultNULLId)
        {
            MailTemplateId = mailTemplateId;
            Prm = prm;

            InitializeComponent();

            StbMailSendTemplateSubject = Common.CreateMailEditTemplateControl(gcSubject.Controls, Appearance.BackColor);
            StbMailSendTemplateBody = Common.CreateMailEditTemplateControl(gcBody.Controls, Appearance.BackColor);
        }

        private void FrmAddEditMailTemplateShown(object sender, EventArgs e)
        {

            if (MailTemplateId == DM4Consts.DefaultNULLId)
            {
                Text = FrmCaption;
            }
            else
            {
                DataRow[] drcoll = FrmMain.DtMailSendTemplates.Select(String.Format("[{0}]={1}", CnMailSendTemplate.Id, MailTemplateId));
                if (drcoll.Length > 0)
                {
                    DataRow dr = drcoll[0];
                    Text = String.Format("Edit mail template: {0}", dr[CnMailSendTemplate.Name]);
                    tbName.Text = dr[CnMailSendTemplate.Name].ToString();
                    StbMailSendTemplateSubject.Text = dr[CnMailSendTemplate.Subject].ToString();
                    StbMailSendTemplateBody.Text = dr[CnMailSendTemplate.Body].ToString();
                }
                else
                {
                    Text = FrmCaption;
                }
            }

            tbName.Focus();
        }

        private void btnOkClick(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(tbName.Text))
                {
                    MessageBox.Show(this, "Mail template name is empty", DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (MailTemplateId == DM4Consts.DefaultNULLId)
                {
                    Result = FrmMain.AddMailSendTemplate(
                        tbName.Text.Trim()
                        , StbMailSendTemplateSubject.Text.Trim()
                        , StbMailSendTemplateBody.Text.Trim());
                }
                else
                {
                    Result = FrmMain.EditMailSendTemplate
                        (MailTemplateId, tbName.Text.Trim()
                        , StbMailSendTemplateSubject.Text.Trim(), StbMailSendTemplateBody.Text.Trim());
                }

                if (Result.Result > 0)
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show(this, Result.Message, DmConsts.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                string m = MailTemplateId == DM4Consts.DefaultNULLId
                    ? "Error of mail template creating"
                    : "Error of mail template editing";
                FrmMain.WriteError(m + Environment.NewLine + ex.Message);
                Close();
            }
        }
    }
}